package bb.lanxing.util.map;

import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.common.config.UmengEventConst;
import bb.lanxing.nav.NavServiceManager;
import bb.lanxing.util.DensityUtil;

public class MapToolBox implements View.OnClickListener {
    private View mapToolBtn;
    private View popupBg;
    private ViewGroup popupContainer;
    private PopupWindow toolBoxPopup;
    private ToolClickListener toolClickListener;
    private boolean showSos = true;

    public interface ToolClickListener {
        void onAltitudeClick();

        void onDistanceClick();

        void onSosClick();
    }

    public MapToolBox(View view, ViewGroup viewGroup, ToolClickListener toolClickListener) {
        mapToolBtn = view;
        popupContainer = viewGroup;
        this.toolClickListener = toolClickListener;
    }

    public void setShowSos(boolean z) {
        showSos = z;
    }

    public void show() {
        Context context = mapToolBtn.getContext();
        int dp2px = DensityUtil.dp2px(context, 320.0f);
        int dp2px2 = DensityUtil.dp2px(context, 140.0f);
        if (toolBoxPopup == null || !toolBoxPopup.isShowing()) {
            toolBoxPopup = new PopupWindow(initToolBoxView(context), dp2px, dp2px2);
            toolBoxPopup.setAnimationStyle(R.style.map_tile_type_popup_anim);
            toolBoxPopup.setFocusable(true);
            toolBoxPopup.setBackgroundDrawable(new BitmapDrawable());
            this.toolBoxPopup.setOnDismissListener(() -> {
                toolBoxPopup = null;
                AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.1f);
                alphaAnimation.setDuration(200L);
                if (popupBg != null) {
                    popupBg.setVisibility(INVISIBLE);
                }
                alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }

                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        if (MapToolBox.this.popupBg != null) {
                            MapToolBox.this.popupBg.setVisibility(INVISIBLE);
                            MapToolBox.this.popupContainer.removeView(MapToolBox.this.popupBg);
                            MapToolBox.this.popupBg = null;
                        }
                    }
                });
                if (MapToolBox.this.popupBg != null) {
                    MapToolBox.this.popupBg.startAnimation(alphaAnimation);
                }
            });
        }
        if (popupBg == null || popupBg.getVisibility() != VISIBLE) {
            popupBg = new View(context);
            popupBg.setBackgroundColor(Integer.MIN_VALUE);
            popupContainer.addView(this.popupBg);
        }
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
        alphaAnimation.setDuration(200L);
        popupBg.setVisibility(INVISIBLE);
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationEnd(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationStart(Animation animation) {
                if (popupBg != null) {
                    popupBg.setVisibility(VISIBLE);
                }
            }
        });
        popupBg.startAnimation(alphaAnimation);
        toolBoxPopup.showAsDropDown(mapToolBtn, (-dp2px) + mapToolBtn.getWidth(), -mapToolBtn.getHeight());
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.altitudeBtn) {
            if (toolClickListener != null) {
                toolClickListener.onAltitudeClick();
            }
            if (toolBoxPopup != null) {
                toolBoxPopup.dismiss();
            }
            MobclickAgent.onEventValue(App.getContext(), UmengEventConst.SPORT_MAP_TOOLBOX_ALTITUDE, null, 1);
        } else if (id == R.id.distanceBtn) {
            if (toolClickListener != null) {
                toolClickListener.onDistanceClick();
            }
            if (toolBoxPopup != null) {
                toolBoxPopup.dismiss();
            }
            MobclickAgent.onEventValue(App.getContext(), UmengEventConst.SPORT_MAP_TOOLBOX_DISTANCE, null, 1);
        } else if (id == R.id.sosBtn) {
            if (toolClickListener != null) {
                toolClickListener.onSosClick();
            }
            if (toolBoxPopup != null) {
                toolBoxPopup.dismiss();
            }
            MobclickAgent.onEventValue(App.getContext(), UmengEventConst.SPORT_MAP_TOOLBOX_SOS, null, 1);
        }
    }

    private View initToolBoxView(Context context) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_sport_map_tool_box, popupContainer, false);
        boolean isNavigating = NavServiceManager.getInstance().isNavigating();
        inflate.findViewById(R.id.altitudeBtn).setOnClickListener(this);
        inflate.findViewById(R.id.distanceBtn).setOnClickListener(this);
        View findViewById = inflate.findViewById(R.id.sosBtn);
        int i = 0;
        if (!showSos) {
            i = 8;
        }
        findViewById.setVisibility(i);
        findViewById.setOnClickListener(this);
        if (isNavigating) {
            int color = context.getResources().getColor(R.color.grey_cccccc);
            ((TextView) inflate.findViewById(R.id.altitudeText)).setTextColor(color);
            ((TextView) inflate.findViewById(R.id.distanceText)).setTextColor(color);
            ((ImageView) inflate.findViewById(R.id.altitudeIcon)).setImageResource(R.drawable.ic_map_altitude_disable);
            ((ImageView) inflate.findViewById(R.id.distanceIcon)).setImageResource(R.drawable.ic_map_length_disable);
        }
        return inflate;
    }
}